// shopPages/personalShop/personalShop.js
var sliderWidth = 33; // 需要设置slider的宽度，用于计算中间位置
var app = getApp();
var page = 1;
var selectedNews = false;
var imageUtil = require('../../utils/img.js');
var imageUtil1 = require('../../utils/img1.js');
var newsList = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["首页", "热销", "新品", "积分折扣"],
    activeIndex: 0, //默认页
    sliderOffset: 0,
    sliderLeft: 0,
    // 数据
    mall_picture: '',
    mall_url: '',
    selected: true,
    goodsList: [],
    isowner: false,
    height: 1200,
    imagewidth: 0, //缩放后的宽
    imageheight: 0, //缩放后的高
    imagewidth1: 0, //缩放后的宽
    imageheight1: 0, //缩放后的高
    imgshow: false,
    fangwname: ''
  },
  imageLoad: function(e) {
    var imageSize = imageUtil.imageUtil(e)

    this.setData({
      imagewidth: imageSize.imageWidth,
      imageheight: imageSize.imageHeight
    })
  },
  imageLoad1: function(e) {
    var imageSize = imageUtil1.imageUtil(e)
    var that = this
    that.setData({
      imagewidth1: imageSize.imageWidth,
      imageheight1: imageSize.imageHeight,
    })
    setTimeout(function() {
      that.setData({
        imgshow: true
      })
    }, 300)
  },
  //点击banner图进入链接页面
  showUlr: function(e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: '../outurl/outurl?url=' + url,
    })
  },
  //点击进入名片
  showCarte: function(e) {
    wx.switchTab({
      url: '/pages/carteShow/carteShow'
    })
  },
  //点击进入详情页面
  bindGoodsTap: function(e) {
    var id = e.detail.button_typId;
    var title = e.detail.button_status;
    wx.navigateTo({
      url: '/pages/goodsDetail/goodsDetail?id=' + id + '&title=' + title,
    })
  },
  //点击购物车图标进入购物车页面
  bindBuyCarTap: function(e) {
    wx.switchTab({
      url: '/pages/buyCar/buyCar?uid=' + app.globalData.userInfo.id
    })
  },
  //获取商品列表
  getGoodsList: function() {
    var that = this;
    var url2 = '/shop_api/index';
    var params2 = {
      uid: app.globalData.SwitchTabParams.v_id,
      pagesize: 20,
      pageno: page
    };
    app.HttpPostSend(url2, params2, function(res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        selectedNews = false;
        that.setData({
          goodsList: []
        })
      } else {
        console.log(res.data.lists);
        var goodsList = res.data.lists;
        var mg_info = res.data.mg_info;
        var fangwname = that.data.fangwname
        console.log(res);
        if (mg_info.com_logo != undefined) {
          that.setData({
            mg_info: mg_info
          })
        }

        for (let i = 0; i < goodsList.length; i++) {
          newsList.push(goodsList[i]);
        }
        if (goodsList.length < 20) {
          selectedNews = false;
        } else {
          selectedNews = true;
          page += 1;
        }
        that.setData({
          goodsList: newsList,
          fangwname: res.data.mg_info.name,
          // imgshow: false
          // mg_info: mg_info
        })
        if (app.globalData.SwitchTabParams.v_id != app.globalData.userInfo.id){
          app.addlog(app.globalData.SwitchTabParams.v_id, that.data.fangwname, app.globalData.userInfo.id, app.globalData.userInfo.username, app.globalData.userInfo.logo, 13, '')
        }
        

        wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    console.log(options)
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    // 页面初始化 options为页面跳转所带来的参数
    if (options.mid != undefined && options.mid != 'undefined') {
      app.globalData.SwitchTabParams.v_id = options.mid;
    }

    // console.log(options);
    // console.log(app.globalData.SwitchTabParams.v_id);
    var url = '/shop/banner';
    var params = {
      uid: app.globalData.SwitchTabParams.v_id
    };
    app.HttpGetSend(url, params, function(res) {
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
      } else {
        if (res.data) {
          that.setData({
            mall_picture: res.data[0].img,
            mall_url: res.data[0].url,
          })
        }

      }
    });
    page = 1;
    selectedNews = false;
    newsList = [];
    that.getGoodsList();

    if (app.globalData.userInfo.id != app.globalData.SwitchTabParams.v_id) {
      // 雷达添加查看记录 -查看商城数
      app.radarAdd(app.globalData.SwitchTabParams.v_id, 4, '', '', app.globalData.userInfo.logo)

      // 发送模板消息-访客到访通知
      app.sendTemplateMessage(app.globalData.SwitchTabParams.v_id, 1, '/radarPages/radarCenter/radarCenter?coming_status=1', '正在访问个人商城', '', '', '')
      this.setData({
        isowner: true
      })
    } else {
      this.setData({
        isowner: false
      })
    }

  },
  tabClick: function(e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  // 点击进去咨询页面
  bindGoSocksTap: function(e) {
    var that = this
    wx.navigateTo({
      url: '/pages/socks/socks?mid=' + this.data.mg_info.id,
    })
    app.addlog(app.globalData.SwitchTabParams.v_id, that.data.fangwname, app.globalData.userInfo.id, app.globalData.userInfo.username, app.globalData.userInfo.logo, 6, '')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // 页面显示


    var that = this;
    var query = wx.createSelectorQuery() //创建节点查询器 query
    query.select('#affix').boundingClientRect() //这段代码的意思是选择Id= the - id的节点，获取节点位置信息的查询请求
    query.exec(function(res) {
      //  console.log(res[0].top);  #affix节点的上边界坐
      that.setData({
        menuTop: res[0].top
      })
    });
  },

  // .监听页面滚动距离scrollTop
  onPageScroll: function(e) {
    // console.log(e.scrollTop);
    var that = this;
    // 3.当页面滚动距离scrollTop > menuTop菜单栏距离文档顶部的距离时，菜单栏固定定位
    if (e.scrollTop > that.data.menuTop) {
      that.setData({
        menuFixed: true
      })
    } else {
      that.setData({
        menuFixed: false
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (selectedNews == true) {
      var that = this;
      // 显示加载图标  
      wx.showLoading({
        title: '玩命加载中',
      })
      // 页数+1  
      that.getGoodsList();

    } else {
      wx.hideLoading();
    }
  }
})